home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_100
/
163_01
/
cc51.c
< prev
next >
Wrap
Text File
|
1990-11-20
|
18KB
|
666 lines
/*
** peephole optimizer
**
** basically a simple text replacement processor
*/
char nooptimize = 0; /* this flag disables optimization if set (by debug) */
peephole(ptr) char *ptr; {
while(*ptr) {
char *ptr1;
ptr1 = ptr; /* save for later compare */
/*
** if optimization is disabled, skip everything
** all patterns start with tab, so look for one
*/
while(*ptr && (*ptr != '\t'|| nooptimize)) cout(*ptr++,output);
if(streq(ptr, "\tLEA AX,[BP]")) ptr = optim1(ptr, 12);
if(streq(ptr, "\tMOV AX,1\n")) ptr = optim3(ptr, 10);
if(streq(ptr, "\tMOV AX,")) ptr = optim4(ptr, 8);
if(streq(ptr, "\tPUSH AX\n\tMOV AX,")) ptr = optim5(ptr, 17);
if(streq(ptr, "\tPUSH AX\n\tLEA AX,[BP]")) ptr = optim6(ptr, 21);
if(streq(ptr, "\tMOV BX,DX\n\tPOP BX\n")) ptr = optim7(ptr, 19);
if(streq(ptr, "\tMOV BX,")) ptr = optim8(ptr, 8);
if(streq(ptr, "\tJNZ $+5\n")) ptr = optim9(ptr+9, "JNZ $+");
if(streq(ptr, "\tJZ $+5\n")) ptr = optim9(ptr+8, "JZ $+");
/* additional optimizing logic goes here */
if(ptr == ptr1) cout(*ptr++, output);
}
}
/*
** optimize strings beginning "\tLEA AX,[BP]"
*/
optim1(ptr, bump) char *ptr; int bump; {
char offset[20], *ptr1, *ptr2;
ptr1 = ptr + bump; /* skip text already recognized */
ptr2 = offset;
while((*ptr2++ = *ptr1++) > ' ') ++bump; /* save offset from LEA */
*--ptr2 = 0;
++bump;
ptr2 = ptr; /* save for compare */
if(streq(ptr1, "\tMOV BX,AX\n\tMOV SI,AX\n\tMOV AX,[SI]\n"))
ptr = optim11(ptr, bump+35, offset);
else if(streq(ptr1, "\tMOV SI,AX\n\tMOV AX,[SI]\n"))
ptr = optim12(ptr, bump+24, offset);
else if(streq(ptr1, "\tMOV SI,AX\n\tMOV AL,[SI]\n")) {
ot("MOV AL,[BP]");
outstr(offset);
outstr(" ;optim1 - 1");
nl();
ptr = ptr1 + 24;
}
else if(streq(ptr1, "\tPUSH AX\n\tLEA AX,[BP]"))
ptr = optim13(ptr, bump+21, offset);
else if(streq(ptr1, "\tPUSH AX\n\tMOV AX,"))
ptr = optim14(ptr, bump+17, offset);
else if(streq(ptr1, "\tPUSH AX\n\tMOV SI,AX\n"))
ptr = optim15(ptr, bump+20, offset);
return ptr;
}
/*
** optimize strings beginning
** "\tLEA AX,[BP]xxxx\n\tMOV BX,AX\n\tMOV SI,AX\n\tMOV AX,[SI]\n"
*/
optim11(ptr, bump, offset) char *ptr, offset[]; int bump; {
char *ptr1, *ptr2;
ptr1 = ptr + bump; /* skip text already recognized */
ptr2 = ptr; /* save for compare */
if(streq(ptr1, "\tINC AX\n\tMOV [BX],AX\n"))
ptr = optim111(ptr, bump+21, offset);
else if(streq(ptr1, "\tDEC AX\n\tMOV [BX],AX\n"))
ptr = optim112(ptr, bump+21, offset);
return ptr;
}
/*
** optimize strings beginning
** "\tLEA AX,[BP]xxxx\n\tMOV BX,AX\n\tMOV SI,AX\n\tMOV AX,[SI]\n"
** "\tINC AX\n\tMOV [BX],AX\n"
*/
optim111(ptr, bump, offset) char *ptr, offset[]; int bump; {
char *ptr1, *ptr2;
ptr1 = ptr + bump; /* skip text already recognized */
ptr2 = ptr; /* save for compare */
if(streq(ptr1, "\tDEC AX\n\tMOV SI,AX\n\tMOV AL,[SI]\n")) {
if(ptr1[32]) { /* if not end of expression */
ot("MOV BX,[BP]");
outstr(offset);
nl();
ot("INC WORD PTR [BP]");
outstr(offset);
nl();
ol("MOV AL,[BX] ;optim111 - 1");
ptr = ptr1 + 32;
}
else
ptr = opti1111(ptr, bump+32, offset);
}
else if(streq(ptr1, "\tDEC AX\n")) {
if(ptr1[8]) { /* if not end of expression */
ot("MOV AX,[BP]");
outstr(offset);
nl();
ot("INC WORD PTR [BP]");
outstr(offset);
outstr(" ;optim111 - 2");
nl();
ptr = ptr1 + 8;
}
else
ptr = opti1111(ptr, bump+8, offset);
}
else if(streq(ptr1, "\tMOV SI,AX\n\tMOV AL,[SI]\n")) {
if(ptr1[8]) { /* if not end of expression */
ot("INC WORD PTR [BP]");
outstr(offset);
nl();
ot("MOV BX,[BP]");
outstr(offset);
nl();
ol("MOV AL,[BX] ;optim111 - 3");
ptr = ptr1 + 24;
}
else
ptr = opti1111(ptr, bump+24, offset);
}
else {
if(ptr1[0]) { /* if not end of expression */
ot("INC WORD PTR [BP]");
outstr(offset);
nl();
ot("MOV AX,[BP]");
outstr(offset);
outstr(" ;optim111 - 4");
nl();
ptr = ptr1;
}
else
ptr = opti1111(ptr, bump, offset);
}
return ptr;
}
/*
** optimize "++" operators on simple auto variables with no destination
*/
opti1111(ptr, bump, offset) char *ptr, offset[]; int bump; {
ot("INC WORD PTR [BP]");
outstr(offset);
outstr(" ;opti1111 - 1");
nl();
return ptr + bump;
}
/*
** optimize strings beginning
** "\tLEA AX,[BP]xxxx\n\tMOV BX,AX\n\tMOV SI,AX\n\tMOV AX,[SI]\n"
** "\tDEC AX\n\tMOV [BX],AX\n"
*/
optim112(ptr, bump, offset) char *ptr, offset[]; int bump; {
char *ptr1, *ptr2;
ptr1 = ptr + bump; /* skip text already recognized */
ptr2 = ptr; /* save for compare */
if(streq(ptr1, "\tINC AX\n\tMOV SI,AX\n\tMOV AL,[SI]\n")) {
if(ptr1[32]) { /* if not end of expression */
ot("MOV BX,[BP]");
outstr(offset);
nl();
ot("DEC WORD PTR [BP]");
outstr(offset);
nl();
ol("MOV AL,[BX] ;optim112 - 1");
ptr = ptr1 + 32;
}
else
ptr = opti1121(ptr, bump+32, offset);
}
else if(streq(ptr1, "\tINC AX\n")) {
if(ptr1[8]) { /* if not end of expression */
ot("MOV AX,[BP]");
outstr(offset);
nl();
ot("DEC WORD PTR [BP]");
outstr(offset);
outstr(" ;optim112 - 2");
nl();
ptr = ptr1 + 8;
}
else
ptr = opti1121(ptr, bump+8, offset);
}
else if(streq(ptr1, "\tMOV SI,AX\n\tMOV AL,[SI]\n")) {
if(ptr1[24]) { /* if not end of expression */
ot("DEC WORD PTR [BP]");
outstr(offset);
nl();
ot("MOV BX,[BP]");
outstr(offset);
nl();
ol("MOV AL,[BX] ;optim112 - 3");
ptr = ptr1 + 24;
}
else
ptr = opti1121(ptr, bump+24, offset);
}
else {
if(ptr1[0]) { /* if not end of expression */
ot("DEC WORD PTR [BP]");
outstr(offset);
nl();
ot("MOV AX,[BP]");
outstr(offset);
outstr(" ;optim112 - 4");
nl();
ptr = ptr1;
}
else
ptr = opti1121(ptr, bump, offset);
}
return ptr;
}
/*
** optimize "--" operators on simple auto variables with no destination
*/
opti1121(ptr, bump, offset) char *ptr, offset[]; int bump; {
ot("DEC WORD PTR [BP]");
outstr(offset);
outstr(" ;opti1121 - 1");
nl();
return ptr + bump;
}
/*
** optimize strings beginning
** "\tLEA AX,[BP]xxxx\n\tMOV SI,AX\n\tMOV AX,[SI]\n"
*/
optim12(ptr, bump, offset) char *ptr, offset[]; int bump; {
char *ptr1, *ptr2;
ptr1 = ptr + bump; /* skip text already recognized */
ptr2 = ptr; /* save for compare */
if(streq(ptr1, "\tMOV SI,AX\n\tMOV AX,[SI]\n")) {
ot("MOV SI,[BP]");
outstr(offset);
nl();
ol("MOV AX,[SI] ;optim12 - 1");
ptr = ptr1 + 24;
}
else if(streq(ptr1, "\tMOV SI,AX\n\tMOV AL,[SI]\n\tCBW\n\tAND AX,AX\n")) {
ot("MOV SI,[BP]");
outstr(offset);
nl();
ol("CMP BYTE PTR [SI],0 ;optim12 - 2");
ptr = ptr1 + 40;
}
else if(streq(ptr1, "\tMOV SI,AX\n\tMOV AL,[SI]\n")) {
ot("MOV SI,[BP]");
outstr(offset);
nl();
ol("MOV AL,[SI] ;optim12 - 3");
ptr = ptr1 + 24;
}
else {
ot("MOV AX,[BP]");
outstr(offset);
outstr(" ;optim12 - 4");
nl();
ptr = ptr1;
}
return ptr;
}
/*
** optimize strings beginning "\tLEA AX,[BP]xxxx\n\tPUSH AX\n\tLEA AX,[BP]"
*/
optim13(ptr, bump, offset) char *ptr, offset[]; int bump; {
char offset2[20], *ptr1, *ptr2;
ptr1 = ptr + bump; /* skip text already recognized */
ptr2 = offset2;
while((*ptr2++ = *ptr1++) > ' ') ++bump; /* save source from LEA */
*--ptr2 = 0;
++bump;
ptr2 = ptr; /* save for compare */
if(streq(ptr1, "\tMOV SI,AX\n\tMOV AX,[SI]\n\tPOP BX\n\tMOV [BX],AX\n")) {
ot("MOV AX,[BP]");
outstr(offset2);
nl();
ot("MOV [BP]");